<!--
 * @Author: lihaogn
 * @Date: 2022-05-18 13:29:56
 * @LastEditTime: 2022-05-18 15:21:22
 * @LastEditor: lihaogn
 * @Description: 
 * @FilePath: \lix-vue2-demos\src\views\demo\apply\file-visit\modules\FileUpload.vue
-->
<template>
  <el-card>
    <div slot="header">上传文件</div>
    <div>
      <input
        type="file"
        ref="fileElemRef"
        multiple
        style="display:none"
        @change="handleFiles"
      />
      <el-button type="primary" @click="selectFile"
        >Select some files</el-button
      >
    </div>
  </el-card>
</template>

<script>
import { uploadRequest } from '../../../../../utils/request'

export default {
  methods: {
    onUploadProgress(e) {
      console.log('e', e)
      if (e.lengthComputable) {
        let percentage = Math.round((e.loaded * 100) / e.total)
        console.log('percent', percentage)
      }
    },
    /**
     * @author: lihaogn
     * @Date: 2022-05-17 15:42:07
     * @description: 处理多个文件的选择
     * @param e - 事件对象
     * @return {*}
     */
    async handleFiles(e) {
      let files = e.target.files
      for (let i = 0; i < files.length; i++) {
        const fd = new FormData()
        fd.append('file', files[i])
        uploadRequest('/upload', fd, this.onUploadProgress).then(res => {
          console.log('res', res)
        })
      }
    },
    /**
     * @author: lihaogn
     * @Date: 2022-05-17 15:42:30
     * @description: 选择按钮点击事件
     * @return {*}
     */
    selectFile() {
      this.$refs.fileElemRef.click()
    }
  }
}
</script>

<style lang="less" scoped></style>
